var searchparam;
var jyz_bh_zx;
/**
 *model 
 */    
    Ext.define('model_jyz', {
         extend: 'Ext.data.Model',
         fields: [
                {name: 'jyz_bh', type: 'string'},
                {name: 'jyz_mc', type: 'string'},
                {name: 'jyz_sh', type: 'string'},
                {name: 'jyz_swjg', type: 'string'},
                {name: 'jyz_swjg_mc', type: 'string'},
                {name: 'jyz_xzqh', type: 'string'},
                {name: 'jyz_xzqh_mc', type: 'string'},
                {name: 'jyz_dz', type: 'string'},
                {name: 'jyz_lxr', type: 'string'},
                {name: 'jyz_dh', type: 'string'},
                {name: 'jyz_bz', type: 'string'},
                {name: 'jyz_swjg_mc', type: 'string'}
         ]
     });
/**
 *store 
 */     
     var store_jyz_zx=Ext.create('Ext.data.Store', {
        model: 'model_jyz',
        autoDestroy: true,
        autoLoad: false,
        pageSize:100,
        listeners:{
            beforeload:function(store,operation,eOpts){//添加事件在获取数据前触发
                if(searchparam){
                    store.proxy.extraParams=searchparam;
                }
            }
        },
        proxy: {
            type: 'ajax',
            url: 'getZXGasListAction.action',
            reader: {
                type: 'json',
                root: 'items',
                totalProperty:'totalCount'
            }
        }
     });
     var store_jyz_pt=Ext.create('Ext.data.Store', {
        model: 'model_jyz',
        autoDestroy: true,
        autoLoad: false,
        pageSize:100,
        listeners:{
            beforeload:function(store,operation,eOpts){//添加事件在获取数据前触发
                if (jyz_bh_zx != "") {
                    store.proxy.extraParams={jyz_bh_zx:jyz_bh_zx};
                };
            }
        },
        proxy: {
            type: 'ajax',
            url: 'getPTGasListAction.action',
            reader: {
                type: 'json',
                root: 'items',
                totalProperty:'totalCount'
            }
        }
     });
 /**
  *grid表格头 
  */    
     var col_jyz=[
        {xtype:'rownumberer'}, 
        {dataIndex:'jyz_swjg_mc',text:'税务机关名称',width:120,menuDisabled:true,sortable:false},
        {dataIndex:'jyz_bh',text:'加油站编号',width:100,menuDisabled:true,sortable:false},
        {dataIndex:'jyz_mc',text:'加油站名称',width:120,menuDisabled:true,sortable:false},
        {dataIndex:'jyz_lxr',text:'法人代表',width:80,menuDisabled:true,sortable:false},
        {dataIndex:'jyz_xzqh_mc',text:'地区名称',width:80,menuDisabled:true,sortable:false},
        {dataIndex:'jyz_sh',text:'纳税人识别号',width:150,menuDisabled:true,sortable:false},
        {dataIndex:'jyz_dh',text:'电话',width:120,menuDisabled:true,sortable:false},
        {dataIndex:'jyz_dz',text:'加油站地址',width:100,menuDisabled:true,sortable:false}
        
     ];

     Ext.onReady(function(){
         Ext.create('Ext.panel.Panel',{
             id:'main-panel',
             width:Ext.getBody().getWidth()-20,
             renderTo:Ext.getBody(),
             layout:{
                 type:'hbox',
                 align: 'stretch'
             },
             items:[{
                        xtype: 'grid',
                        id:'grid_jyz_zx',
                        title:'中心加油站信息',
                        width:500,
                        height:650,
                        //selModel: Ext.create('Ext.selection.CheckboxModel', { mode: "SIMPLE" }),
                        columns: col_jyz,            
                        store: store_jyz_zx,
                        listeners:{
                            itemclick:function(view,record,item,index,e,eOpts){
                                jyz_bh_zx=record.raw.jyz_bh;
                                store_jyz_pt.loadPage(1);
                            }
                        },
                        dockedItems:[
                            {
                                xtype: 'toolbar',
                                items:[{
                                    xtype:'button',
                                    text:'查询',
                                    margin:'10 10 0 10',
                                    width:100,
                                    handler:showSearchDlg
                                }]
                            },{
                                xtype: 'pagingtoolbar',
                                store: store_jyz_zx,   
                                dock: 'bottom',
                                displayInfo: true
                            }
                        ], 
                        flex: 1  
             },{
                        xtype: 'grid',
                        id:'grid_jyz_pt',
                        title:'普通加油站信息',
                        height:650,
                        //selModel: Ext.create('Ext.selection.CheckboxModel', { mode: "SIMPLE" }),
                        columns: col_jyz,            
                        store: store_jyz_pt,
                        dockedItems:[
                            {
                                xtype: 'toolbar',
                                items:[{
                                    xtype:'button',
                                    text:'新增关联',
                                    margin:'10 10 0 10',
                                    width:100,
                                    handler:function(){
                                        var g=Ext.getCmp('grid_jyz_zx');
                                        var m=g.getSelectionModel().getSelection();
                                        if (m.length>0) {
                                            showNewDlg();
                                        }else{
                                            Ext.Msg.alert('提示信息','请先选择中心站，再关联普通站。');
                                        };
                                    }
                                },{
                                    xtype:'button',
                                    text:'删除关联',
                                    margin:'10 10 0 10',
                                    width:100,
                                    handler:function(){
                                        var g=Ext.getCmp('grid_jyz_pt');
                                        var m=g.getSelectionModel().getSelection();
                                        if (m.length>0) {
                                            showDelDlg(m[0].raw);
                                        }else{
                                            Ext.Msg.alert('提示信息','请选中要删除的加油站！');
                                        };
                                    }
                                }]
                            },{
                                xtype: 'pagingtoolbar',
                                store: store_jyz_pt,   
                                dock: 'bottom',
                                displayInfo: true
                            }
                        ], 
                        flex: 1  
             }]
         });
     });

     function showSearchDlg(){
         var dlg=Ext.create('Ext.window.Window',{
             title:'查询条件',
             width:350,
             height:250,
             layout:'fit',
             resize:false,
             modal:true,
             items:[
                    Ext.create('Ext.form.Panel',{
                        bodyPadding:5,
                        //width:700,
                        layout:'anchor',
                        items:[
                            {
                                xtype:'textfield',
                                fieldLabel:'加油站税号',
                                name: 'jyz_sh',
                                labelWidth:80,
                                width:300
                            },{
                                xtype:'treecombobox',
                                fieldLabel:'行政区划',
                                name: 'jyz_xzqh',
                                valueField:'id',
                                url: 'getAreaListAction.action',
                                labelWidth:80,
                                width:300
                            }
                     ],
                     buttons:[{
                            text:'提交',
                            handler:function(){
                                    searchparam=this.up('form').getForm().getValues();
                                    store_jyz_zx.loadPage(1);
                                    dlg.close();
                            }
                        },{
                            text:'重置',handler:function(){this.up('form').getForm().reset();}
                        },{
                            text:'关闭',handler:function(){dlg.close();}
                    }]
                })
             ]
         });
         dlg.show();
     }
     function showNewDlg(){
         var dlg=Ext.create('Ext.window.Window',{
             title:'新增关联普通加油站',
             width:500,
             height:450,
             layout:'fit',
             resize:false,
             modal:true,
             items:[
                    Ext.create('Ext.form.Panel',{
                        bodyPadding:5,
                        //width:700,
                        layout:'anchor',
                        items:[
                            {
                                xtype:'treepanel',
                                title:'选择普通加油站',
                                id:'ptzxz',
                                width:460,
                                height:300,
                                store : new Ext.data.TreeStore({
                                    root : {
                                        expanded : true
                                    },
                                    proxy : {
                                        type : 'ajax',
                                        url : 'getAllPTGasListAction.action'
                                    },
                                    autoLoad : true
                                }),
                                rootVisible : false
                            },{
                                xtype:'textfield',
                                fieldLabel:'加油站编号',
                                id:'jyz_bh_pt',
                                name: 'jyz_bh_pt',
                                hidden:true,
                                labelWidth:80,
                                width:300,
                                allowBlank: false
                            }
                     ],
                     buttons:[{
                            text:'提交',
                            handler:function(){
                                    var ids=[],checkData=Ext.getCmp('ptzxz').getView().getChecked();
                                    Ext.Array.each(checkData,function(obj){ids.push(obj.raw.id);});
                                    Ext.getCmp('jyz_bh_pt').setValue(ids.join(','));
                                    var f=this.up('form').getForm();
                                    if (f.isValid()) {
                                        f.submit({
                                            url:'addPTGasAction.action',
                                            params:{jyz_bh_zx:jyz_bh_zx},
                                            clientValidation:false,
                                            waitMsg:'正在提交数据,请稍候...',
                                            success: function(form, action){
                                                Ext.Msg.alert('提示信息',action.result.msg); 
                                                store_jyz_pt.loadPage(1);
                                                dlg.close();
                                            },
                                            failure: function(form, action) {
                                                Ext.Msg.alert('提示信息',action.result.msg); 
                                            }
                                        });
                                    };
                            }
                        },{
                            text:'重置',handler:function(){this.up('form').getForm().reset();}
                        },{
                            text:'关闭',handler:function(){dlg.close();}
                    }]
                })
             ]
         });
         dlg.show();
     }
     function showDelDlg(raw){
         var dlg=Ext.create('Ext.window.Window',{
             title:'删除关联普通加油站',
             width:350,
             height:250,
             layout:'fit',
             resize:false,
             modal:true,
             items:[
                    Ext.create('Ext.form.Panel',{
                        bodyPadding:5,
                        //width:700,
                        layout:'anchor',
                        items:[
                            {
                                xtype:'textfield',
                                fieldLabel:'加油站编号',
                                name: 'jyz_bh_pt',
                                value:raw.jyz_bh,
                                readOnly:true,
                                labelWidth:80,
                                width:300,
                                allowBlank: false
                            },{
                                xtype:'textfield',
                                fieldLabel:'加油站名称',
                                name: 'jyz_mc',
                                value:raw.jyz_mc,
                                readOnly:true,
                                labelWidth:80,
                                width:300,
                                allowBlank: false
                            }
                     ],
                     buttons:[{
                            text:'提交',
                            handler:function(){
                                    var f=this.up('form').getForm();
                                    if (f.isValid()) {
                                        f.submit({
                                            url:'delPTGasAction.action',
                                            params:{jyz_bh_zx:jyz_bh_zx},
                                            clientValidation:false,
                                            waitMsg:'正在提交数据,请稍候...',
                                            success: function(form, action){
                                                Ext.Msg.alert('提示信息',action.result.msg); 
                                                store_jyz_pt.loadPage(1);
                                                dlg.close();
                                            },
                                            failure: function(form, action) {
                                                Ext.Msg.alert('提示信息',action.result.msg); 
                                            }
                                        });
                                    };
                            }
                        },{
                            text:'重置',handler:function(){this.up('form').getForm().reset();}
                        },{
                            text:'关闭',handler:function(){dlg.close();}
                    }]
                })
             ]
         });
         dlg.show();
     }